<?php
/* 用户表 */
namespace app\console\model;
use think\Db;
use think\Model;

class MessageBox extends Model {


    protected $tableName = 'message_box';
    
    const MESSAGE_STATUS_DRAFT=1;//存草稿箱
    const MESSAGE_STATUS_OK=2;//发布状态
    const MESSAGE_STATUS_DELETE=3;//删除
    
    /*
     * 获取即将要要推动的消息
     */
    public function getPushedMessage(){
        //2017-10-12 11:01:00
        //2017-10-12 11:03:59
        $startMinute=1507802760;//strtotime(date('YmdHi'.'00',strtotime('-1 Minute')));
        $endMinute=strtotime(date('YmdHi'.'59',strtotime('+1 Minute')));
        $condtion=[/*'release_type'=>2*/'status'=>2,'is_release'=>0,'release_time'=>[['EGT',$startMinute],['ELT',$endMinute]]];
        return Db::name($this->tableName)->where($condtion)->select();
    }
    
    /*
     * 通过ID获取某条数据
     */
    public function getOneMessageBoxDataByID($id)
    {
        return Db::name($this->tableName)->where(['id'=>$id])->find();
    }
    /*
     * 插入单条数据
     */
    public  function insertMessageBox($data)
    {
        Db::name($this->tableName)->insert($data);
        return Db::name($this->tableName)->getLastInsID();
    }
    /*
     * 获取消息列表
     */
    public function getMessageBoxList($condition,$paginate=[],$orderBy=[]){
        return  Db::name($this->tableName)->where($condition)->order($orderBy)->paginate($paginate);
    }
    
    /*
     * 通过消息ID更新status
     */
    public function updateStatusByID($msgID,$status){
        if(!(int)$msgID) return false;
        return Db::name($this->tableName)->where(['id'=>$msgID])->update(['status'=>$status]);
    }
   
    /*
     * 通过消息ID更新数据
     */
    public function updateDataByID($msgID,$data){
        if(!(int)$msgID) return false;
        return Db::name($this->tableName)->where(['id'=>$msgID])->update($data);
    }
    
    /*
     * 添加消息点击量
     */
    public function setClickAmountIncByID($msgId){
        if (!(int)$msgId)return false;
        Db::name($this->tableName)->where(['id'=>$msgId])->setInc('click_amount');
    }
    
    /*
     * 添加消息转发量
     */
    public function setForwardAmountIncByID($msgId){
        if (!(int)$msgId)return false;
        Db::name($this->tableName)->where(['id'=>$msgId])->setInc('forward_amount');
    }
    
    /*
     * 更改消息发送标志为已经发送
     */
    public function updateIsreleaseSendByID($msgId) {
        if (!(int)$msgId)return false;
        return Db::name($this->tableName)->where(['id'=>$msgId])->setField('is_release',1);
    }
}